package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.SpuCommentMapper;
import com.bush.mall.product.entity.SpuCommentEntity;
import com.bush.mall.product.service.SpuCommentService;

/**
 * 商品评价
 * @author RUN
 * @date 2022-11-28
 */
@Service(SpuCommentService.BEAN_ID)
@Slf4j
public class SpuCommentServiceImpl extends ServiceImpl<SpuCommentMapper, SpuCommentEntity> implements SpuCommentService {

    /**
     * 查询商品评价列表
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<SpuCommentEntity> page = new Page<>(current,size);
            QueryWrapper<SpuCommentEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("SpuCommentService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建商品评价表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveSpuComment(SpuCommentEntity spuComment) {
        try{
            Assert.notNull(spuComment,"参数异常");
            boolean flag = this.save(spuComment);
            if (flag) return Result.ok("商品评价添加成功");
            throw new SaaSException("商品评价添加失败");
        }catch(Exception e){
            log.error("SpuCommentService saveSpuComment ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改商品评价表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateSpuComment(SpuCommentEntity spuComment) {
        try{
            Assert.notNull(spuComment,"参数异常");
            boolean flag = this.updateById(spuComment);
            if (flag) return Result.ok("商品评价修改成功");
            throw new SaaSException("商品评价修改失败");
        }catch(Exception e){
            log.error("SpuCommentService updateSpuComment ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除商品评价表单
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result deleteSpuComment(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("商品评价删除成功");
            throw new SaaSException("商品评价删除失败");
        }catch(Exception e){
            log.error("SpuCommentService deleteSpuComment ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}